<?php

// +----------------------------------------------------------------------
// | 订单 模型
// +----------------------------------------------------------------------
// | @author fancsen
// +----------------------------------------------------------------------

namespace app\api\model;

use app\api\model\BaseModel;

class Order extends BaseModel 
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'order';
    //自动时间戳
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'delete_time';
    //隐藏字段
    protected $hidden = ['user_id','delete_time','update_time'];

    public function getSnapItemsAttr($value)
    {
        if(empty($value)){
            return null;
        }
        return json_decode($value);
    }

    public function getSnapAddressAttr($value)
    {
        if(empty($value)){
            return null;
        }
        return json_decode($value);
    }

    /**
     * 获取用户订单分页数据
     */
    public static function getSummaryByUser($uid,$page=1,$size=15)
    {
        $orders = self::where('user_id','=',$uid)
            ->order('create_time desc')
            ->paginate($size,true,['page'=>$page]);

        return $orders;
    }
    /**
     * 分页获取所有订单
     */
    public static function getSummaryByPage($page=1, $size=20){
        $pagingData = self::order('create_time desc')
            ->paginate($size, true, ['page' => $page]);
        return $pagingData ;
    }

}